package com.adxon.data;

import javax.servlet.http.HttpServletRequest;

import com.adxon.MsgException;

public class User extends Individual{
	private String acc;
	private String cfm;
	private com.google.appengine.api.users.User gus;
	private String pwc;					// current password
	private String pwn;					// new password
	private UserEx usx=new UserEx();	// user extra

	public void checkNewPass()throws MsgException{
		if(pwn==null || !pwn.equals(cfm))
			throw new MsgException(MsgException.NOT_MATCH,"f");
	}
	public void checkPassword()throws MsgException{
		if(pwc==null || !usx.chkPassword(pwc))
			throw acc==null?new MsgException(MsgException.PASSWORD,"p"):
				new MsgException(MsgException.ACCOUNT,"a");
	}
	@Override
	public void commit()throws MsgException{
		if(id==0){
			// for new account
			boolean x=false;
			try{
				usx.update();			// check existing
				x=true;
			}catch(MsgException e){}
			if(x)
				throw new MsgException(MsgException.EXIST,null);
		}
		super.commit();
		usx.id=id;
		if(acc!=null)
			usx.pk=acc;
		if(pwn!=null)
			usx.setPassword(pwn);
		usx.commit();
	}
	public String getAccount(){
		return acc;
	}
	@Override
	public void getAll(HttpServletRequest req){
		super.getAll(req);
		req.setAttribute("a",acc);
		req.setAttribute("f",cfm);
		req.setAttribute("n",pwn);
		req.setAttribute("p",pwc);
	}
	public String getPassword(){
		return pwc;
	}
	public void google(com.google.appengine.api.users.UserService google){
		gus=google.getCurrentUser();
		rk=google.isUserAdmin()?10:90;
		tx=gus.getNickname();
	}
	@Override
	public void reset(){
		super.reset();
		usx.reset();
	}
	public void setAccount(String account){
		acc=usx.pk=regulate(account);
	}
	public void setAll(HttpServletRequest req)throws MsgException{
		super.setAll(req);
		acc=regulate(req.getParameter("a"));
		cfm=regulate(req.getParameter("f"));
		pwc=regulate(req.getParameter("p"));
		pwn=regulate(req.getParameter("n"));
	}
	public void setNewPass(String password){
		pwn=regulate(password);
	}
	public void setPassword(String password){
		pwc=regulate(password);
	}
	@Override
	public void setTime(long time){
		super.setTime(time);
		usx.tm=time;
	}
	@Override
	public User update()throws MsgException{
		if(id==0 && acc==null)
			throw new MsgException(MsgException.ACCOUNT,"a");
		if(id==0)
			usx.pk=acc;
		else usx.id=id;
		usx.update();
		id=usx.id;
		super.update();
		return this;
	}
	@Override
	public User update(long id)throws MsgException{
		this.id=id;
		return update();
	}
}

class UserEx extends Data{
	private String pw;	// password
	private String sl;	// salt for password

	public UserEx(){
		bsk="i11";
	}
	@Override
	protected String allocateId(){
		return pk;
	}
	boolean chkPassword(String pwc){
		return pwc.equals(pw);
	}
	@Override
	protected void commitExt()throws MsgException{
		super.commitExt();
		commit("pw",pw);
		commit("sl",sl);
	}
	void setPassword(String pwn){
		pw=sl=pwn;
	}
	@Override
	public UserEx update()throws MsgException{
		super.update();
		pw=updateString("pw");
		sl=updateString("sl");
		return this;
	}
}